package com.jnu.student;

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jnu.student.data.Bill;
import com.jnu.student.data.BillDataBank;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class BillActivity extends AppCompatActivity {
    private ArrayList<Bill> bills = new ArrayList<>();
    private BillAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bill);

        RecyclerView recyclerView = findViewById(R.id.recycle_view_bill);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        bills= new BillDataBank().loadBills(this);
        // 反转数据集合
        Collections.reverse(bills);
        adapter = new BillAdapter(bills);
        recyclerView.setAdapter(adapter);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.bill, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_clear) {
            // 创建对话框询问是否清空
            new AlertDialog.Builder(this)
                    .setTitle("确认清空")
                    .setMessage("是否确认清空账单？")
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // 执行清空操作的逻辑
                            bills.clear();
                            new BillDataBank().saveBills(BillActivity.this, bills);
                            // 通知适配器数据集发生变化
                            adapter.notifyDataSetChanged();
                        }
                    })
                    .setNegativeButton(android.R.string.no, null)
                    .show();

            return true;
        } else {
            return super.onOptionsItemSelected(item);
        }
    }

    // 将 BillAdapter 类移到 BillActivity 类的内部
    public static class BillAdapter extends RecyclerView.Adapter<BillAdapter.BillViewHolder> {
        private final List<Bill> bills;

        public BillAdapter(List<Bill> dataList) {
            this.bills = dataList;
        }

        @NonNull
        @Override
        public BillViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.billl_item_row, parent, false);
            return new BillViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(@NonNull BillViewHolder holder, int position) {
            Bill data = bills.get(position);
            holder.bindData(data);
        }

        @Override
        public int getItemCount() {
            return bills.size();
        }

        // 将 BillViewHolder 类声明为静态内部类
        public static class BillViewHolder extends RecyclerView.ViewHolder {
            private final TextView textViewTitle;
            private final TextView textViewAmount;
            private final TextView dateTextView;

            public BillViewHolder(@NonNull View itemView) {
                super(itemView);
                textViewTitle = itemView.findViewById(R.id.bill_item);
                textViewAmount =itemView.findViewById(R.id.amount_point);
                dateTextView =itemView.findViewById(R.id.dateTextView);
            }

            public void bindData(Bill data) {
                textViewTitle.setText(data.getTitle());
                dateTextView.setText((data.getDay())+"日");
                textViewAmount.setText("" + data.getAmount());

                if (data.getAmount() >= 0) {
                    textViewAmount.setTextColor(Color.parseColor("#FF0099CC")); // 浅蓝色
                } else {
                    textViewAmount.setTextColor(Color.parseColor("#F84683")); // 浅红色
                }
            }
        }
    }
}